<?php
/**
 * rakuten乐天产品
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\RakutenAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlat;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;

class ProductRakutenSku extends Model{

    protected $table = 'product_rakuten_sku';

    protected $guarded = [];

    public static $_fanEu = [
        1 => '泛欧',
        0 => '非泛欧'
    ];

    public static $_barcodeType = [
        1 => 'UPC码',
        2 => 'EAN码',
        3 => '无',
    ];

    const STATUS_YES = 1; //产品启用
    const STATUS_NO  = 2; //产品停用
    public static $_status = [
        self::STATUS_YES => '启用',
        self::STATUS_NO  => '停用',
    ];

    /**
     * 是否为海外仓产品
     */
    const OS_WAREHOUSE_YES = 1; //海外仓产品
    const OS_WAREHOUSE_NO  = 2; //非海外仓产品
    public static $_isOsWarehouse = [
        self::OS_WAREHOUSE_YES => '是',
        self::OS_WAREHOUSE_NO  => '否',
    ];

    /**
     * 是否混合库存（fba+海外仓）
     */
    const IS_MIX_YES = 1; //是
    const IS_MIX_NO  = 2; //否
    public static $_isMixInventory = [
        self::IS_MIX_YES => '是',
        self::IS_MIX_NO  => '否',
    ];

    public static function displayStatus() {
        return ['1' => '<i class="fa fa-check text-green"></i>', '2' => '<i class="fa fa-close text-red"></i>'];
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function account(){
        return $this->belongsTo(RakutenAccount::class,'account_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public static function getValueByCode($code, $field){
        return self::query()->where('seller_sku', $code)->value($field);
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    public static function getDownloadData($res)
    {
        foreach ($res as $model){
            $temp = [
                $model->seller_sku ?? '',
                $model->sku->psku_code ?? '',
                $model->sku->psku_cn,
                ProductClassify::$_season[$model->skuClassify->season ?? 3],
                $model->skuClassify->title ?? '',
                $model->account->account ?? '',
                $model->country->country ?? '',
                User::getUsernameById($model->seller_id),
                self::$_barcodeType[$model->barcode_type],
                $model->barcode,
                User::getUsernameById($model->cuser_id),
                $model->created_at,
                self::$_status[$model->status],
            ];
            yield $temp;
        }
    }

}
